<?php
class Forum_Helper_Url
{
    /**
     * Singleton instance
     * @var Forum_Helper_Url
     */
    private static $_instance = null;
    
    /**
     * Retrieve instance of Forum_Helper_Url
     * 
     * @return Forum_Helper_Url
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * Retrieve path to the skin directory of current template
     * 
     * @param string $postfix
     * @return string
     */
    public function getSkinUrl($postfix = '')
    {
        return $this->getServerUrl() . 
            $this->getBaseUrl() . '/skin/' . 
            Forum::getConfig()->design->template . '/' . $postfix;
    }
    
    /**
     * Retrieve path to the js directory
     * 
     * @param string $postfix
     * @return string
     */
    public function getJsUrl($postfix = '')
    {
        return $this->getServerUrl() . 
            $this->getBaseUrl() . '/js/' . $postfix;
    }
    
    public function getUrl($url = '')
    {
        return $this->getServerUrl() . $this->getBaseUrl() . '/' . $url;
    }
    
    /**
     * Retrieve site base url
     * 
     * @return string
     */
    public function getBaseUrl()
    {
        $script = trim($_SERVER['SCRIPT_NAME'], '/');
        if (strstr($script, '/')) {
            return '/' . dirname($script);
        }
        return '';
    }
    
    /**
     * Retrieve site root url
     * 
     * @return string
     */
    public function getServerUrl()
    {
        $url = 'http';
        if (!empty($_SERVER['HTTPS'])) {
            $url .= 's';
        }
        return $url .= '://' . $_SERVER['SERVER_NAME'];
    }
    
    /**
     * Retrieve current url
     * 
     * @return string
     */
    public function getSelfUrl() {
        $url = $this->getServerUrl();
        if ($_SERVER['SERVER_PORT'] != '80') {
            $url .= ':' . $_SERVER['SERVER_PORT'];
        }
        $url .= $_SERVER['REQUEST_URI'];
        return $url;
    }
}